package main.service.mail.db;

import main.service.mail.bean.LastTimeBean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Date;

public class Db {
    private static Connection conn;

    public static Connection getConn() {
        if (conn == null) {
            connect();
        }
        return conn;
    }

    public static void execute(String s) {
        try {
            Statement state = Db.getConn().createStatement();
            state.executeUpdate(s);
            state.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void connect() {
        try {
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:mail.db");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        JDBCMail.create();
        JDBCLastTime.insert("1", new Date());
        LastTimeBean bean = JDBCLastTime.find("1");
        System.out.println(bean.getLastTime());
        JDBCMail.create();
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        JDBCMail.insert("subject", "from","receiveAddress","sentDate","priority");
        main();
    }

    public static void main() {
        JDBCMail.drop();
        JDBCLastTime.drop();
        JDBCLastTime.create();
        JDBCMail.create();
    }
}
